package com.ruoyi.wechat.service.impl;

import java.util.List;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.wechat.domain.Result;
import com.ruoyi.wechat.utils.CacheClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.wechat.mapper.TeacherMapper;
import com.ruoyi.wechat.domain.Teacher;
import com.ruoyi.wechat.service.ITeacherService;

import static com.ruoyi.wechat.redis.RedisConstants.*;
import static com.ruoyi.wechat.redis.RedisConstants.CACHE_TEACHER_TTL;

/**
 * 讲师列表Service业务层处理
 *
 * @author 林道敬
 * @date 2025-01-10
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper,Teacher> implements ITeacherService
{
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private CacheClient cacheClient;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 查询讲师列表
     *
     * @param id 讲师列表主键
     * @return 讲师列表
     */
    @Override
    public Teacher selectTeacherById(String id)
    {
        Teacher teacher = cacheClient
                .queryWithPassThrough(CACHE_TEACHER_KEY, id, Teacher.class,this::getById, CACHE_TEACHER_TTL, TimeUnit.MINUTES);
        return teacherMapper.selectTeacherById(id);
    }

    /**
     * 查询讲师列表列表
     *
     * @param teacher 讲师列表
     * @return 讲师列表
     */
    @Override
    public List<Teacher> selectTeacherList(Teacher teacher)
    {
        return teacherMapper.selectTeacherList(teacher);
    }

    /**
     * 新增讲师列表
     *
     * @param teacher 讲师列表
     * @return 结果
     */
    @Override
    public int insertTeacher(Teacher teacher)
    {
        teacher.setCreateTime(DateUtils.getNowDate());
        return teacherMapper.insertTeacher(teacher);
    }

    /**
     * 修改讲师列表
     *
     * @param teacher 讲师列表
     * @return 结果
     */
    @Override
    public int updateTeacher(Teacher teacher)
    {

        // 2.删除缓存
        stringRedisTemplate.delete(CACHE_TEACHER_KEY + teacher.getId());
        teacher.setUpdateTime(DateUtils.getNowDate());
        return teacherMapper.updateTeacher(teacher);
    }

    /**
     * 批量删除讲师列表
     *
     * @param ids 需要删除的讲师列表主键
     * @return 结果
     */
    @Override
    public int deleteTeacherByIds(String[] ids)
    {
        return teacherMapper.deleteTeacherByIds(ids);
    }

    /**
     * 删除讲师列表信息
     *
     * @param id 讲师列表主键
     * @return 结果
     */
    @Override
    public int deleteTeacherById(String id)
    {
        return teacherMapper.deleteTeacherById(id);
    }
}
